<template>
    <div>
        <div class="header">
            <span class="fa fa-chevron-left" @click="back" id="back"></span>
            <span>头像上传</span>
            <span></span>
        </div>
        <div id="avatar">
             <label for="avatar-input" id="avatar-label">+</label>
            <input type="file" accept="image/*" id="avatar-input" style="display:none" @change="chooseImg">
            <button @touchstart="commit">上传</button>
        </div>
        <div>
           
        </div>

    </div>
</template>

<script>
import {mapState} from 'vuex';
export default {
    ...mapState([
          "userinfo"
    ]),
    methods:{
        back:function(){
            history.back();
        },
        chooseImg:function(e){
            // console.dir(e.target.files);
            //获得所选择的文件对象
            this.pics = e.target.files[0];
            //获得文件的url（本地url）
            var url = URL.createObjectURL(this.pics);
            var label =document.getElementById("avatar-label");
            label.style.backgroundImage = "url("+url+")";
            label.textContent = "";
        },
        commit:function(){
            var data = new FormData();
            data.append("avatar",this.pics);
            this.http.post("/api/users/upload",data)
            .then(res=>{
                layer.msg(res.data.msg);
                this.$router.push({path:'/mine'})
            });
        }
    }
}
</script>

<style scoped>
    .header {
        padding: 7px 15px;
        width: 100vw;
        height: 44px;
        background-color: #cccccc;
        position: fixed;
        display: flex;
        justify-content: space-between;
        align-items: center;
        top: 0;
        left: 0;
        font-size:20px;
    }
    #avatar{
        margin-top:56px;
    }
   #avatar-label{
        display: block;
        border: solid 2px black;
        width: 200px;
        height: 200px;
        margin: 0 auto;
        background-size: 100% 100%;
        font-size:150px;    
    }
    button{
        width:100%;
        height:40px;
        border:none;
        margin-top:15px;
        background-color:red;
        color:white;
        font-size:18px;
    }
</style>